package com.xcity.game.activity.recharge;

import java.util.Arrays;
import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.game.activity.ActivityService;
import com.xcity.game.activity.limitbuy.DailyLimitBuyActivityService;
import com.xcity.game.activity2.ActivityIds;
import com.xcity.game.common.Cause;
import com.xcity.game.common.ItemResponse;
import com.xcity.game.common.MessageConst;
import com.xcity.game.common.PlayerPool;
import com.xcity.game.event.GameEvents;
import com.xcity.game.recharge.RechargeTemplate;
import com.xcity.pomelo.PomeloRoute;
import com.xcity.pomelo.push.msg.ActivityStatePush;
import com.xcity.pomelo.request.ActivityRechargeGiftInfoRequest;
import com.xcity.pomelo.request.ActivityRechargeGiftRewardRequest;
import com.xcity.pomelo.response.ActivityRechargeGiftInfoResponse;
import com.xcity.pomelo.response.ActivityRechargeGiftRewardResponse;
import com.xcity.pomelo.response.HintResponse;
import com.xcity.pomelo.response.Response;

import naga.x.App;
import naga.x.event.Event;
import naga.x.event.EventListener;
import naga.x.game.template.TemplateService;
import naga.x.net.Session;
import naga.x.net.annotation.OPHandler;
import naga.x.net.impl.pomelo.annotation.PomeloOP;
import naga.x.service.ServiceException;

/**
 * 充值礼包活动
 * @author yang.li
 *
 */
@OPHandler
public class RechargeGiftActivityService extends ActivityService implements EventListener {
	
	private RechargeTemplate[] templates;
	private List<ItemResponse>[] items; // 对应奖励
	private int[] ids;
	
	@SuppressWarnings("unchecked")
	@Override
	public void startup() throws ServiceException {
		templates = new RechargeTemplate[] {
				App.getApp().getServiceManager().get(TemplateService.class).get(RechargeTemplate.class, 10001), // 1元
				App.getApp().getServiceManager().get(TemplateService.class).get(RechargeTemplate.class, 30003), // 6元
				App.getApp().getServiceManager().get(TemplateService.class).get(RechargeTemplate.class, 30013), // 30元
		};
		items = new List[] {
				Arrays.asList(new ItemResponse(80214, 1), new ItemResponse(20023, 200000), new ItemResponse(30109, 3)),
				Arrays.asList(new ItemResponse(30104, 1), new ItemResponse(20023, 650000), new ItemResponse(30107, 2)),
				Arrays.asList(new ItemResponse(30144, 1), new ItemResponse(20023, 5000000), new ItemResponse(30110, 2)),
		};
		ids = new int[] { ActivityIds.ACTIVITY_RECHARGE_GIFT1, ActivityIds.ACTIVITY_RECHARGE_GIFT2, ActivityIds.ACTIVITY_RECHARGE_GIFT3 };
		App.getApp().getEventManager().register(this);
	}

	@Override
	public void shutdown() throws ServiceException {
	}

	@Override
	public int[] getEventTypes() {
		return new int[] { GameEvents.EVENT_PLAYER_LOADED, GameEvents.EVENT_RECHARGE, GameEvents.EVENT_RECHARGE_OFFLINE };
	}

	@Override
	public void handleEvent(Event event) {
		switch (event.getType()) {
			case GameEvents.EVENT_PLAYER_LOADED:
				playerLoaded(event.getParameter());
				break;
			case GameEvents.EVENT_RECHARGE:
			case GameEvents.EVENT_RECHARGE_OFFLINE:
				onRecharge(event.getParameter(0), event.getParameter(2));
				break;
		}
	}
	
	private void playerLoaded(Player player) {
		int n = player.getPool().getIntValue(PlayerPool.PROPERTY_ACTIVITY_RECHARGE_GIFT);
		int index = n >> 8 & 0xff;
		int state = n & 0xff;
		if (index < templates.length && state != DailyLimitBuyActivityService.STATE_RECEIVED) {
			player.send(new ActivityStatePush(ids[index], STATE_OPENED));
		}
	}
	
	private void onRecharge(Player player, RechargeTemplate template) {
		int n = player.getPool().getIntValue(PlayerPool.PROPERTY_ACTIVITY_RECHARGE_GIFT);
		int index = n >> 8 & 0xff;
		int state = n & 0xff;
		if (state == DailyLimitBuyActivityService.STATE_NO_BUY
				&& template.getPrice() >= templates[index].getPrice()) {
			state = DailyLimitBuyActivityService.STATE_BOUGHT & 0xff;
			player.poolSet(PlayerPool.PROPERTY_ACTIVITY_RECHARGE_GIFT, (index << 8) | state);
		}
	}
	
	@PomeloOP(PomeloRoute.ACTIVITY_RECHARGE_GIFT)
	protected void requestInfo(Session session, ActivityRechargeGiftInfoRequest req) {
		Player player = session.getClient();
		int n = player.getPool().getIntValue(PlayerPool.PROPERTY_ACTIVITY_RECHARGE_GIFT);
		int index = n >> 8 & 0xff;
		int state = n & 0xff;
		RechargeTemplate template = templates[index];
		Response res = new ActivityRechargeGiftInfoResponse(req.getSerial(), (byte) state, template, items[index]);
		session.send(res);
	}
	
	@PomeloOP(PomeloRoute.ACTIVITY_RECHARGE_GIFT_GET_REWARD)
	protected void requestReward(Session session, ActivityRechargeGiftRewardRequest req) {
		Player player = session.getClient();
		int n = player.getPool().getIntValue(PlayerPool.PROPERTY_ACTIVITY_RECHARGE_GIFT);
		int index = n >> 8 & 0xff;
		int state = n & 0xff;
		if (state == DailyLimitBuyActivityService.STATE_NO_BUY) {
			session.send(new HintResponse(req.getSerial(), MessageConst.GET_REWARD_WHEN_NO_BUY));
			return;
		}
		if (state == DailyLimitBuyActivityService.STATE_RECEIVED) {
			session.send(new HintResponse(req.getSerial(), MessageConst.REWARD_ALREADY_RECEIVED));
			return;
		}
		state = DailyLimitBuyActivityService.STATE_RECEIVED & 0xff;
		List<ItemResponse> rewards = items[index];
		for (ItemResponse item : rewards) {
			player.getBag().addItemWithoutException(item.getId(), item.getCount(), Cause.ACTIVITY_RECHARGE_GIFT);
		}
		session.send(new ActivityStatePush(ids[index], STATE_CLOSED));
		int nextIndex = index;
		if (index < templates.length - 1) { // 下一个档位
			++nextIndex;
			state = DailyLimitBuyActivityService.STATE_NO_BUY;
			session.send(new ActivityStatePush(ids[nextIndex], STATE_OPENED));
		}
		player.poolSet(PlayerPool.PROPERTY_ACTIVITY_RECHARGE_GIFT, (nextIndex << 8) | state);
		session.send(new ActivityRechargeGiftRewardResponse(req.getSerial(), (byte) state, templates[nextIndex], items[nextIndex]));
		LOG.info("[ACTIVITY RECHARGE GIFT]ID[{}]TID[{}]INDEX[{}]", player.getId(), templates[index].getId(), index);
	}

}
